package com.csxy.news.util;

import java.util.ArrayList;
import java.util.List;

/**
 * 实体分页类
 * @author Administrator
 *
 * @param <T>
 */
public class Pager<T> {

	//总的记录数，指定数据库中的总记录
	private long totalCount;
	//每页要显示的记录数，称其为页的大小
	private int pageSize;
	//共有多省页
	private int pageCount;
	//当前的页号：从1开始计算
	private int pageIndex;
	
	//当前页所显示的实体对象列表
	private List<T> pageDataList;

	private String showJsp="index.jsp";
	
	public Pager(long totalCount, int pageSize,int pageIndex) {
		super();
		this.totalCount = totalCount;
		this.pageSize = pageSize;
		//计算共有多少页
		float v=this.totalCount/(float)this.pageSize;
		this.pageCount=(int)Math.ceil(v);//向上取整
		this.pageIndex=pageIndex;
		this.pageDataList=new ArrayList<T>();
	}

	public Pager(long totalCount, int pageSize) {
		
		this(totalCount,pageSize,1);
		
	}
	
	public Pager() {
		
		this(0,0,0);
		
	}
	
	public void setShowJsp(String showJsp) {
		this.showJsp = showJsp;
	}

	public int getIndex() {
		return pageIndex;
	}

	public void setIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public List<T> getBeanList() {
		return pageDataList;
	}

	public void setBeanList(List<T> pageDataList) {
		this.pageDataList = pageDataList;
	}

	public long getOffset() {
		
		return (this.pageIndex-1)*this.pageSize;
		
	}
	
    //获取指定页的第一条记录与数据库中第一条记录的偏移量
	public long getOffset(int pageIndex) {
		
		this.pageIndex=pageIndex;
		
		return (this.pageIndex-1)*this.pageSize;
		
	}
	
	private StringBuffer buffer=new StringBuffer();
	//获取分页汇总信息
	public String getTitle(){
		this.buffer.setLength(0);
		
		this.buffer.append("<span>");
		this.buffer.append("共").append(this.pageCount).append("页，当前是第").append(this.pageIndex).append("页");
		this.buffer.append("</span>");		
		
		return this.buffer.toString();
	}
	
	//判断是否有上一页
	public boolean getHasPrev(){
		return this.pageIndex-1>0;
	}
	//判断是否有下一页
	public boolean getHasNext(){
		return this.pageIndex+1<=this.pageCount;
	}
	
	//返回最后页的链接
	public String getFirstUrl(){
		
		String href=String.format("%s?%s=%d&%s=%d",this.showJsp,
				WebUtil.PAGE_INDEX,
				1,
				WebUtil.PAGE_SIZE,
				this.pageSize);
		return href;
	}
	
	//获取当前页的链接
	public String getCurUrl(){

		String href=String.format("%s?%s=%d&%s=%d",this.showJsp,
				  WebUtil.PAGE_INDEX,
				  this.pageIndex,
				  WebUtil.PAGE_SIZE,
				  this.pageSize);
		return href;
	}
	//返回上一页的链接
	public String getPrevUrl(){
		
		if(this.pageIndex-1<1){
			return "javascript:void(0);";
		}
		
		String href=String.format("%s?%s=%d&%s=%d",this.showJsp,
				  WebUtil.PAGE_INDEX,
				  this.pageIndex-1,
				  WebUtil.PAGE_SIZE,
				  this.pageSize);
		return href;
	}
	
	//返回下一页的链接
	public String getNextUrl(){
		
		if(this.pageIndex+1>this.pageCount){
			return "javascript:void(0);";
		}
		
		String href=String.format("%s?%s=%d&%s=%d",this.showJsp,
				WebUtil.PAGE_INDEX,
				this.pageIndex+1,
				WebUtil.PAGE_SIZE,
				this.pageSize);
		return href;
	}
	
	//返回最后页的链接
	public String getLastUrl(){
		
		String href=String.format("%s?%s=%d&%s=%d",this.showJsp,
				WebUtil.PAGE_INDEX,
				this.pageCount,
				WebUtil.PAGE_SIZE,
				this.pageSize);
		return href;
	}
	//获取导航链接
	public String getNavUrl(){
		this.buffer.setLength(0);
		
		this.buffer.append("<ul class='pagination pagination-lg'>");
		/*
		<li><a href="#">第一页</a></li>
        <li><a href="#">上一页</a></li>
	    <li><a href="#">下一页</a></li>
	    <li><a href="#">最后页</a></li>
		 */
		if(this.pageIndex==1){
		   //当前已经是第一页，则没有下一页导航
		  
		  this.buffer.append("<li class='disable'><a href='javascript:void(0);'>第一页</a></li>"); 
		  this.buffer.append("<li><a href='").append(getNextUrl()).append("' >下一页</a></li>"); 
		  this.buffer.append("<li><a href='").append(getLastUrl()).append("' >最后页</a></li>");
		  
		   
		}else if(this.pageIndex==this.pageCount){
			//已经是最后一页，则没有下一页导航
			this.buffer.append("<li><a href='").append(getFirstUrl()).append("' >第一页</a></li>"); 
			this.buffer.append("<li><a href='").append(getNextUrl()).append("' >下一页</a></li>");
			this.buffer.append("<li class='disable'><a href='javascript:void(0);'>最后页</a></li>");
		}else{
			
			this.buffer.append("<li><a href='").append(getFirstUrl()).append("' >第一页</a></li>"); 
			this.buffer.append("<li><a href='").append(getPrevUrl()).append("' >上一页</a></li>");
			this.buffer.append("<li><a href='").append(getNextUrl()).append("' >下一页</a></li>");
			this.buffer.append("<li><a href='").append(getLastUrl()).append("' >最后页</a></li>");
			  
		}
		
		this.buffer.append("</ul>");
		
		return this.buffer.toString();
	}

	//定位到指定的页
	public int locate(int pageIndex) {
		if(pageIndex<1){
			this.pageIndex=1;
		}else if(pageIndex>this.pageCount){
			this.pageIndex=this.pageCount;
		}
		return this.pageIndex;
	}

	//定位到最后一页
	public int locateLast() {
		this.pageIndex=this.pageCount;
		return this.pageIndex;
	}
	
	public int pageIndex(){
		
		return this.pageIndex;
		
	}
	
	
}
